import type { BaseSearchModel } from "@/types";
import { type PropsWithChildren, useState } from "react";
import { ListViewContext } from "./context";

type ListViewProps<TSearchModel extends BaseSearchModel> = PropsWithChildren<{
	searchModel?: TSearchModel;
}>;

export default function ListView<
	TSearchModel extends BaseSearchModel = BaseSearchModel,
>({ children, searchModel }: ListViewProps<TSearchModel>) {
	const [model, setModel] = useState<TSearchModel>(
		searchModel ?? ({} as TSearchModel),
	);

	return (
		<div className="kl-list-view">
			<ListViewContext.Provider
				value={{ searchModel: model, setSearchModel: setModel }}
			>
				{children}
			</ListViewContext.Provider>
		</div>
	);
}
